package online.westonline.com.westonline.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gcssloop.widget.RCRelativeLayout;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.entry.IamgeBean;
import online.westonline.com.westonline.entry.NodeBean;
import online.westonline.com.westonline.utils.GsonUtils;
import online.westonline.com.westonline.utils.UiUtlis;

/**
 * 便签适配器
 * Created by xiatao on 2018/6/11.
 */

public class NodeAdpter extends RecyclerView.Adapter<NodeAdpter.NodeViweholder> {

    private boolean selected=false;
    private static final int MYLIVE_MODE_CHECK = 0;
    int mEditMode = MYLIVE_MODE_CHECK;

    private List<NodeBean> nodeBeans = new ArrayList<>();
    private onNodeClicklistener clicklistener;
    private onNodeLongClickListener longClickListener;
    private boolean isallcheck = false;
    private int isshow = 0;
    private int checkpos;
    private int checksize;
    private boolean onecheck = false;

    public List<NodeBean> getNodeBeans() {
        return nodeBeans;
    }

    public void setClicklistener(onNodeClicklistener clicklistener) {
        this.clicklistener = clicklistener;
    }

    public void setLongClickListener(onNodeLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }
    public void setEditMode(int editMode) {
        mEditMode = editMode;
        notifyDataSetChanged();
    }
    public void setNodeBeans(List<NodeBean> nodeBeans) {
        this.nodeBeans = nodeBeans;
    }

    @NonNull
    @Override
    public NodeViweholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.node_item, parent, false);
        return new NodeViweholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NodeViweholder holder, final int position) {
        NodeBean bean = nodeBeans.get(position);
        if (mEditMode == MYLIVE_MODE_CHECK) {
            holder.checkboxTodelete.setVisibility(View.GONE);
        } else {
            holder.checkboxTodelete.setVisibility(View.VISIBLE);
        }
        if (bean.getCheck())
        {
            holder.checkboxTodelete.setChecked(true);
        }
        else
        {
            holder.checkboxTodelete.setChecked(false);
        }
        holder.tvNode.setText(bean.getTime());
        if (!TextUtils.isEmpty(bean.getTitle())) {
            holder.tvNodeTitle.setText(bean.getTitle());
        }
        else
        {
            holder.tvNodeTitle.setText("(未命名)"+bean.getType());
        }
        if (!TextUtils.isEmpty(bean.getImg_paths()))
        {
            //注意有图片了！
            //隐藏顶部栏
            holder.nodeStatebar.setVisibility(View.GONE);
            IamgeBean photos = GsonUtils.parseJSON(bean.getImg_paths(),IamgeBean.class);
            if (photos.getPaths().size()>1)
            {
                holder.tvNodeImgcunt.setText("+"+photos.getPaths().size());
            }
            else
            {
                holder.node_imgstate.setVisibility(View.GONE);
            }
                Glide.with(holder.itemView.getContext())
                        .load(new File(photos.getPaths().get(0)))
                        .into(holder.imgNodeItem);
        }
        else
        {
            //设置随机颜色
            holder.nodeStatebar.setBackgroundColor(UiUtlis.RandomColor());
            holder.raMyimg.setVisibility(View.GONE);
        }
        holder.tvNodeContent.setText(bean.getContent());
        holder.checkboxTodelete.setChecked(bean.isCheck());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clicklistener != null) {
                    clicklistener.onClick(position,nodeBeans);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (longClickListener != null) {
                    longClickListener.longClick(position,nodeBeans);
                }
                return true;
            }
        });

    }

    @Override
    public int getItemCount() {
        return nodeBeans.size();
    }

    public class NodeViweholder extends RecyclerView.ViewHolder {
        @BindView(R.id.checkbox_todelete)
        CheckBox checkboxTodelete;
        @BindView(R.id.node_statebar)
        RCRelativeLayout nodeStatebar;
        @BindView(R.id.node_imgstate)
        View node_imgstate;
        @BindView(R.id.tv_node_imgcunt)
        TextView tvNodeImgcunt;
        @BindView(R.id.img_node_item)
        ImageView imgNodeItem;
        @BindView(R.id.ra_myimg)
        RCRelativeLayout raMyimg;
        @BindView(R.id.tv_node_title)
        TextView tvNodeTitle;
        @BindView(R.id.tv_node_content)
        TextView tvNodeContent;
        @BindView(R.id.tv_node)
        TextView tvNode;

        public NodeViweholder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    public interface onNodeLongClickListener {
        void longClick(int pos, List<NodeBean> datas);
    }

    public interface onNodeClicklistener {
        void onClick(int pos, List<NodeBean> datas);
    }

}
